<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="css/index.css" />
</head>

<body>
    <div id="typeadd">
        <h2>添加房屋类型</h2>
        <el-form ref="loginform" :model="housesTypeObj" label-width="100px" style="width:500px">
            <!-- <el-form-item label="房屋类型编号">
                <el-input v-model="housesTypeObj.typeId"></el-input>
            </el-form-item> -->
            <el-form-item label="房屋类型":required="true">
                <el-input v-model="housesTypeObj.typeName"clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="insert">添加</el-button>
            </el-form-item>
        </el-form>

    </div>
    <script type="text/javascript" src="js/vue.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/axios.min.js"></script>

    <script type="text/javascript" src="js/common.js"></script>
    <script type="text/javascript">
        new Vue({
            el: "#typeadd",
            data: {
                housesTypeObj: {
                    typeId: "",
                    typeName: "",
                },
                typeList:[]
            },
            methods: {
                insert: function () {

                    let that = this;
                    var newArr;
                    let fm = new FormData();
                    axios.get("/housesType/selectAll").then(function (r) {
                        if (r.data.state == "success") {
                            var str = "";
                            that.typeList = r.data.housestypelist;
                            that.typeList.forEach(function (e) {
                                str += e.typeName + ",";
                            });
                            str = str.substring(0, str.length - 1);
                            newArr = str.split(",");

                            //window.console.log(newArr);
                            if ( that.housesTypeObj.typeName + '' == "") {
                                that.$message.error("输入信息不完整，请检查！");
                            }
                            else if (newArr.indexOf(that.housesTypeObj.typeName + '') !== -1) {
                               that.$message({
                                    message:"类型已存在，请重新输入！",
                                    type:"error"
                                })

                            }
                            else {
                                 that.$message({
                                    message: "添加类型成功！",
                                    type: "success",
                                    duration: 1000
                                });
                                // fm.append("typeId", that.housesTypeObj.typeId);
                                fm.append("typeName", that.housesTypeObj.typeName);

                                axios.post("/housesType/insert", fm).then(function (r) {
                                    if (r.data.state == "success") {
                                       
                                        setTimeout("location.href = 'housesTypeList.html'",1000);

                                    }
                                });
                            }


                        }

                    });

                    // let fm = new FormData();
                    // fm.append("typeId", this.housesTypeObj.typeId);
                    // fm.append("typeName", this.housesTypeObj.typeName);

                    // axios.post("/housesType/insert", fm).then(function (r) {
                    //     if (r.data.state == "success") {
                    //         location.href = "housesTypeList.html";
                    //     }
                    // });
                }
            }
        });


    </script>

</body>

</html>